<?php 
namespace app\index\validate;
use think\Validate;
use think\Db;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 商品验证器
 */
class Goods extends Validate{
	protected $rule = [
	    ['goodsSn','require|max:20|checkGoodsSn:1','请输入产品编号|产品编号不能超过20个字符'],
	    ['goodsName'  ,'require|max:100','请输入产品名称|产品名称不能超过50个字符'],
        ['goodsUnit'  ,'require|max:10','请输入产品单位|产品单位不能超过5个字符']
    ];

    protected $scene = [
        'add'   =>  ['goodsSn','goodsName','goodsUnit'],
        'edit'  =>  ['goodsSn','goodsName','goodsUnit']
    ]; 
    
    protected function checkGoodsSn($value){
        $where = [];
        $where['goodsSn'] = $value;
        $goodsId = Input('post.goodsId/d',0);
        if($goodsId)$where['goodsId'] = ['neq',$goodsId];
        $rs = Db::name('Goods')->where($where)->count();
        return ($rs==0)?true:'该产品编号已存在';
    }
}